#!/bin/bash
#install common tools
source /etc/profile
yum -y install rpcbind nfs-utils bridge-utils chrony ipvsadm ipset sysstat conntrack libseccomp wget tcpdump screen vim bind-utils wget socat telnet sshpass net-tools sysstat lrzsz yum-utils device-mapper-persistent-data lvm2 tree nc lsof strace nmon iptraf iftop mlocate ipvsadm jq
#disable NetworkManager
systemctl stop NetworkManager
systemctl disable NetworkManager
#start chrony
systemctl enable chronyd.service
systemctl start chronyd.service
#优化内核参数
cat > /etc/sysctl.d/k8s.conf <<-EOF
net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-ip6tables=1
net.ipv4.ip_forward=1
vm.swappiness=0
vm.overcommit_memory=1
vm.panic_on_oom=0
fs.inotify.max_user_instances=8192
fs.inotify.max_user_watches=1048576
fs.file-max=52706963
fs.nr_open=52706963
net.ipv6.conf.all.disable_ipv6=1
net.netfilter.nf_conntrack_max=2310720
EOF
sysctl -p

#永久新增br_netfilter模块
cat > /etc/rc.sysinit << EOF
#!/bin/bash
for file in /etc/sysconfig/modules/*.modules ; do
[ -x $file ] && $file
done
EOF

cat > /etc/sysconfig/modules/br_netfilter.modules << EOF
   modprobe br_netfilter
EOF
chmod 755 /etc/sysconfig/modules/br_netfilter.modules

##安装ipvs模块
cat > /etc/sysconfig/modules/ipvs.modules <<EOF
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack_ipv4
EOF
chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep -e ip_vs -e nf_conntrack_ipv4
chmod 755 /etc/sysconfig/modules/br_netfilter.modules
#修改limit file
[ $(cat /etc/security/limits.conf|grep '* soft nproc 10240000'|wc -l) -eq 0 ] && echo '* soft nproc 10240000' >>/etc/security/limits.conf
[ $(cat /etc/security/limits.conf|grep '* hard nproc 10240000'|wc -l) -eq 0 ] && echo '* hard nproc 10240000' >>/etc/security/limits.conf
[ $(cat /etc/security/limits.conf|grep '* soft nofile 10240000'|wc -l) -eq 0 ] && echo '* soft nofile 10240000' >>/etc/security/limits.conf
[ $(cat /etc/security/limits.conf|grep '* hard nofile 10240000'|wc -l) -eq 0 ] && echo '* hard nofile 10240000' >>/etc/security/limits.conf
#关闭防火墙，禁止selinux
systemctl disable firewalld
systemctl stop firewalld
sed -i '/SELINUX=/s/enforcing/disabled/' /etc/selinux/config
setenforce 0
#关闭swap
/usr/sbin/swapoff -a
sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
#reset iptables
yum install -y iptables-services
/usr/sbin/iptables -P FORWARD ACCEPT
/usr/sbin/iptables -X
/usr/sbin/iptables -F -t nat
/usr/sbin/iptables -X -t nat
#添加hosts文件
cat >>/etc/hosts <<-EOF
172.19.158.154 master001 k8s-master01.xhaihua.cn
172.19.158.153 master002 k8s-master02.xhaihua.cn
172.19.158.155 master003 k8s-master03.xhaihua.cn
172.19.158.156 worker001 k8s-worker01.xhaihua.cn
172.19.158.157 worker002 k8s-worker01.xhaihua.cn
EOF

#新增k8s的yum源
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

yum clean all
yum -y makecache


